package com.ganee.agora.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.alibaba.android.arouter.launcher.ARouter;
import com.ganee.agora.event.PostEvent;
import com.ganee.agora.model.FileEntity;
import com.ganee.agora.model.HomeListBean;
import com.ganee.agora.model.UserInfoEntity;
import com.ganee.agora.repository.ProfileRepository;
import com.ganee.baselib.arouter.Constance;
import com.ganee.baselib.httprequest.callback.RequestCallback;
import com.ganee.baselib.httprequest.callback.RequestMultiplyCallback;
import com.ganee.baselib.httprequest.exception.ApiException;
import com.ganee.baselib.mvvmlibrary.base.BaseViewModel;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import okhttp3.RequestBody;

public class ProfileViewModel extends BaseViewModel<ProfileRepository> {
    public MutableLiveData<UserInfoEntity> getUserInfoEntity = new MutableLiveData<>();
    public MutableLiveData<String> downLoadUserPic = new MutableLiveData<>();
    public MutableLiveData<String> upLoadUserPic = new MutableLiveData<>();
    public MutableLiveData<FileEntity> uploadFile = new MutableLiveData<>();

    public ProfileViewModel(@NonNull Application application) {
        super(application);
    }

    @Override
    protected ProfileRepository getRepository() {
        return new ProfileRepository(this);
    }


    //获取用户信息
    public void getUserInfo() {
        mRepository.getUserInfo(new RequestCallback<UserInfoEntity>() {

            @Override
            public void onSuccess(UserInfoEntity userInfoEntity) {
                getUserInfoEntity.setValue(userInfoEntity);
            }
        });
    }

    //更新用户信息
    public void updateUserInfo(RequestBody body) {
        mRepository.updateUserInfo(body,new RequestCallback<UserInfoEntity>() {

            @Override
            public void onSuccess(UserInfoEntity userInfoEntity) {
                finish();
                EventBus.getDefault().post(new PostEvent("profile"));
            }
        });
    }

    //更新用户头像
    public void uploadUserPic(RequestBody body) {
        mRepository.uploadUserPic(body,new RequestCallback<String>() {
            @Override
            public void onSuccess(String url) {
                upLoadUserPic.setValue(url);
            }
        });
    }

    //更新用户头像
    public void uploadFile(RequestBody body) {
        mRepository.uploadFile(body,new RequestCallback<FileEntity>() {
            @Override
            public void onSuccess(FileEntity url) {
                uploadFile.setValue(url);
            }
        });
    }

}
